# -*- coding:utf-8 -*-
import abc

from database import MYSQL


class Const():
    ROW_MAX = 9
    LINE_MAX = 8
    db = MYSQL()
    ai_chess = {'MAX_VAL': 99999, 'MAIN_VAL': 8888}
    levelDict = [
        '学1-1', '学1-2', '学2-1', '学2-2', '学2-3', '学2-4', '学2-5', '学2-6', '学2-7',
        '学2-8', '学2-9', '业1-1', '业1-2', '业1-3', '业1-4', '业1-5', '业1-6', '业1-7',
        '业1-8', '业1-9', '业2-1', '业2-2', '业2-3', '业2-4', '业2-5', '业2-6', '业2-7',
        '业2-8', '业2-9', '业3-1', '业3-2', '业3-3', '业3-4', '业3-5', '业3-6', '业3-7',
        '业3-8', '业3-9', '业4-1', '业4-2', '业4-3', '业4-4', '业4-5', '业4-6', '业4-7',
        '业4-8', '业4-9', '业5-1', '业5-2', '业5-3', '业5-4', '业5-5', '业5-6', '业5-7',
        '业5-8', '业5-9', '业6-1', '业6-2', '业6-3', '业6-4', '业6-5', '业6-6', '业6-7',
        '业6-8', '业6-9', '业7-1', '业7-2', '业7-3', '业7-4', '业7-5', '业7-6', '业7-7',
        '业7-8', '业7-9', '业8-1', '业8-2', '业8-3', '业8-4', '业8-5', '业8-6', '业8-7',
        '业8-8', '业8-9', '业9-1', '业9-2', '业9-3', '业9-4', '业9-5', '业9-6', '业9-7',
        '业9-8', '业9-9', '神1-1', '神1-2', '神1-3', '神1-4', '神1-5', '神2-0', '神2-1'
    ]
    detail_val = {
        'z': [9,  9,  9, 11, 13, 11,  9,  9,  9,
		19, 24, 34, 42, 44, 42, 34, 24, 19,
		19, 24, 32, 37, 37, 37, 32, 24, 19,
		19, 23, 27, 29, 30, 29, 27, 23, 19,
		14, 18, 20, 27, 29, 27, 20, 18, 14,
		7,  0, 13,  0, 16,  0, 13,  0,  7,
		7,  0,  7,  0, 15,  0,  7,  0,  7,
		0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0], # 卒
        'h': [2,  0,  3, 0, 4, 0,  3,  0,  2,
		0, 25, 0, 42, 0, 42, 0, 25, 0,
		20, 0, 33, 0, 37, 0, 33, 0, 20,
		0, 23, 0, 30, 0, 30, 0, 23, 0,
		15, 0, 21, 0, 29, 0, 21, 0, 15,
		0,  13, 0,  14, 0,  14, 0,  13,  0,
		9,  0,  9,  0, 10,  0,  9,  0,  9,
		0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0,
		0,  0,  0,  0,  0,  0,  0,  0,  0], # 火
        'c': [206, 208, 207, 213, 214, 213, 207, 208, 206,
		206, 212, 209, 216, 233, 216, 209, 212, 206,
	    206, 208, 207, 214, 216, 214, 207, 208, 206,
		206, 213, 213, 216, 216, 216, 213, 213, 206,
		208, 211, 211, 214, 215, 214, 211, 211, 208,
		208, 212, 212, 214, 215, 214, 212, 212, 208,
		204, 209, 204, 212, 214, 212, 204, 209, 204,
		198, 208, 204, 212, 212, 212, 204, 208, 198,
		200, 208, 206, 212, 200, 212, 206, 208, 200,
		194, 206, 204, 212, 200, 212, 204, 206, 194], # 车
        'e': [209, 211, 210, 216, 217, 216, 210, 211, 209,
        209, 215, 212, 219, 236, 219, 212, 215, 209,
        209, 211, 210, 217, 219, 217, 210, 211, 209,
        209, 216, 216, 219, 219, 219, 216, 216, 209,
        211, 214, 214, 217, 218, 217, 214, 214, 211,
        211, 215, 215, 217, 218, 217, 215, 215, 211,
        207, 212, 207, 215, 217, 215, 207, 212, 207,
        201, 211, 207, 215, 215, 215, 207, 211, 201,
        203, 211, 209, 215, 203, 215, 209, 211, 203,
        197, 209, 207, 215, 203, 215, 207, 209, 197], # 猛
        'q': [207, 209, 208, 214, 215, 214, 208, 209, 207,
        207, 213, 210, 217, 234, 217, 210, 213, 207,
        207, 209, 208, 215, 217, 215, 208, 209, 207,
        207, 214, 214, 217, 217, 217, 214, 214, 207,
        209, 212, 212, 215, 216, 215, 212, 212, 209,
        209, 213, 213, 215, 216, 215, 213, 213, 209,
        205, 210, 205, 213, 215, 213, 205, 210, 205,
        199, 209, 205, 213, 213, 213, 205, 209, 199,
        201, 209, 207, 213, 201, 213, 207, 209, 201,
        195, 207, 205, 213, 201, 213, 205, 207, 195], # 枪
        'm': [90, 90, 90, 96, 90, 96, 90, 90, 90,
		90, 96,103, 97, 94, 97,103, 96, 90,
		92, 98, 99,103, 99,103, 99, 98, 92,
		93,108,100,107,100,107,100,108, 93,
		90,100, 99,103,104,103, 99,100, 90,
		90, 98,101,102,103,102,101, 98, 90,
		92, 94, 98, 95, 98, 95, 98, 94, 92,
		93, 92, 94, 95, 92, 95, 94, 92, 93,
		85, 90, 92, 93, 78, 93, 92, 90, 85,
		88, 85, 90, 88, 90, 88, 90, 85, 88], # 马
        'f': [92, 92, 92, 98, 92, 98, 92, 92, 92,
        92, 98, 105, 99, 96, 99, 105, 98, 92,
        94, 100, 101, 105, 101, 105, 101, 100, 94,
        95, 110, 102, 109, 102, 109, 102, 110, 95,
        92, 102, 101, 105, 106, 105, 101, 102, 92,
        92, 100, 103, 104, 105, 104, 103, 100, 92,
        94, 96, 100, 97, 100, 97, 100, 96, 94,
        95, 94, 96, 97, 94, 97, 96, 94, 95,
        87, 92, 94, 95, 80, 95, 94, 92, 87,
        90, 87, 92, 90, 92, 90, 92, 87, 90], # 飞
        'p': [100, 100, 96, 91, 90, 91,  96, 100, 100,
		98,  98,  96, 92,  89, 92,  96,  98,  98,
		97,  97,  96, 91,  92, 91,  96,  97,  97,
		96,  99,  99, 98, 100, 98,  99,  99,  96,
		96,  96,  96, 96, 100, 96,  96,  96,  96,
		95,  96,  99, 96, 100, 96,  99,  96,  95,
		96,  96,  96, 96,  96, 96,  96,  96,  96,
		97,  96, 100, 99, 101, 99, 100,  96,  97,
		96,  97,  98, 98,  98, 98,  98,  97,  96,
		96,  96,  97, 99,  99, 99,  97,  96,  96], # 炮
        'y': [150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150,
		150, 150, 150, 150,  150, 150,  150, 150, 150], # 妖
        'x': [0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0,23, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		18,0, 0, 0,23, 0, 0, 0,18,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0], # 相
        'g': [0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0,23, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		18,0, 0, 0,23, 0, 0, 0,18,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0,20, 0, 0, 0,20, 0, 0], # 龟
        's': [0, 0, 0,20, 0,20, 0, 0, 0,
		0, 0, 0, 0,23, 0, 0, 0, 0,
		0, 0, 0,20, 0,20, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0,20, 0,20, 0, 0, 0,
		0, 0, 0, 0,23, 0, 0, 0, 0,
		0, 0, 0,20, 0,20, 0, 0, 0], # 士
        'j': [0, 0, 0, 8888, 8888, 8888, 0, 0, 0,
		0, 0, 0, 8888, 8888, 8888, 0, 0, 0,
		0, 0, 0, 8888, 8888, 8888, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 8888, 8888, 8888, 0, 0, 0,
		0, 0, 0, 8888, 8888, 8888, 0, 0, 0,
		0, 0, 0, 8888, 8888, 8888, 0, 0, 0] # 将
    }

const = Const()

class Chess():
    position = []  #分别对应棋子所在的行与列
    name = ''
    skillName = None

    def __init__(self, side):
        self.side = side    #0代表红方/1代表黑方

    def __str__(self):
        return ''

    def setSkill(self, sName):
        self.skillName = sName

    def setPosition(self, position):
        self.position = position

    def getSide(self):
        return self.side

    def getName(self):
        return self.name

    def calculate(board):
        return 0

class Rooks(Chess): #车
    def __init__(self, side):
        super().__init__(side)
        self.name = '車'

    def __str__(self):
        if self.side == 0:
            return 'r_c'
        else:
            return 'b_c'

    def search(self, board, pos, paramDirArr):
        row, line = pos
        dirArr = paramDirArr
        ret = []
        if row != dirArr[0]:
            increase = dirArr[0] - row
            row += increase
            while row >= 0 and row <= const.ROW_MAX:
                if board[row][line] is not None:
                    if board[row][line].getSide() == self.getSide():
                        return ret
                    else:
                        ret.append([row, line])
                        return ret
                ret.append([row, line])
                row += increase
        else:
            increase = dirArr[1] - line
            line += increase
            while line >= 0 and line <= const.LINE_MAX:
                if board[row][line] is not None:
                    if board[row][line].getSide() == self.getSide():
                        return ret
                    else:
                        ret.append([row, line])
                        return ret
                ret.append([row, line])
                line += increase
        return ret

    def calculate(self, board):
        res = []
        row, line = self.position
        temp = self.search(board, self.position, [row + 1, line])
        for item in temp:
            res.append(item)
        temp = self.search(board, self.position, [row, line + 1])
        for item in temp:
            res.append(item)
        temp = self.search(board, self.position, [row - 1, line])
        for item in temp:
            res.append(item)
        temp = self.search(board, self.position, [row, line - 1])
        for item in temp:
            res.append(item)
        return res

    def isLegal(board, target):
        return True


class Demon(Chess): #妖

    def __init__(self, side):
        super().__init__(side)
        self.name = '妖'
        rooks = Rooks(side) #实例化所有Chess子类
        mao = Mao(side)
        fire = Fire(side)
        el = Elephants(side)
        ma = Mandarins(side)
        ma.act = True
        king = King(side)
        pao = Pao(side)
        pawns = Pawns(side)
        tu = Turtle(side)
        gun = Gun(side)
        meng = Meng(side)
        fei = Fei(side)
        self.chessDict = {
            Rooks: rooks, Mao: mao, Fire: fire, King: king, Pao: pao, Gun: gun,
            Fei: fei, Elephants: el, Mandarins: ma, Pawns: pawns, Turtle: tu, Meng: meng
        }

    def __str__(self):
        if self.side == 0:
            return 'r_y'
        else:
            return 'b_y'

    def meet(self, position): #获取相接位置列表(十字)
        ret = []
        row, line = position
        if row + 1 <= const.ROW_MAX:
            ret.append([row + 1, line])
        if row - 1 >= 0:
            ret.append([row - 1, line])
        if line + 1 <= const.LINE_MAX:
            ret.append([row, line + 1])
        if line - 1 >= 0:
            ret.append([row, line - 1])
        return ret

    def calculate(self, board):
        res = []
        for item in self.meet(self.position):
            temp = board[item[0]][item[1]]
            if temp is not None and type(temp) is not Demon: #禁止套娃
                self.chessDict[type(temp)].setPosition(self.position)
                for it in self.chessDict[type(temp)].calculate(board):
                    if it not in res:
                        res.append(it)
        return res

class Fire(Chess): #火
    def __init__(self, side):
        super().__init__(side)
        self.name = '火'

    def __str__(self):
        if self.side == 0:
            return 'r_h'
        else:
            return 'b_h'

    def calculate(self, board):
        res = []
        row, line = self.position
        if self.side == 0:
            if row + 1 <= const.ROW_MAX and line - 1 >= 0:
                if board[row+1][line-1] is None or board[row+1][line-1].getSide() == 1:
                    res.append([row + 1, line - 1])
            if row + 1 <= const.ROW_MAX and line + 1 <= const.LINE_MAX:
                if board[row+1][line+1] is None or board[row+1][line+1].getSide() == 1:
                    res.append([row + 1, line + 1])
        else:
            if row - 1 >= 0 and line - 1 >= 0:
                if board[row-1][line-1] is None or board[row-1][line-1].getSide() == 0:
                    res.append([row - 1, line - 1])
            if row - 1 >= 0 and line + 1 <= const.LINE_MAX:
                if board[row-1][line+1] is None or board[row-1][line+1].getSide() == 0:
                    res.append([row - 1, line + 1])
        return res

class Mao(Chess): #马
    def __init__(self, side):
        super().__init__(side)
        self.name = '馬'

    def __str__(self):
        if self.side == 0:
            return 'r_m'
        else:
            return 'b_m'

    def calculate(self, board):
        res = []
        row, line = self.position
        allList = [[row+2, line+1], [row-2, line+1], [row+2, line-1], [row-2, line-1],\
        [row+1, line+2], [row-1, line+2], [row+1, line-2], [row-1, line-2]]
        for item in allList:
            if self.isLegal(board, item):
                res.append(item)
        return res
    def isLegal(self, board, destination):
        dx, dy = destination
        row, line = self.position
        if dx >= 0 and dx <= const.ROW_MAX and dy >= 0 and dy <= const.LINE_MAX:
            if board[dx][dy] is not None and board[dx][dy].getSide() == self.side:
                return False
            if abs(row - dx) == 1 and board[row][int((line + dy) / 2)] is None:
                return True
            if abs(line - dy) == 1 and board[int((row + dx) / 2)][line] is None:
                return True
        return False

class Fei(Mao): # 飞
    def __init__(self, side):
        super().__init__(side)
        self.name = '迎'

    def __str__(self):
        if self.side == 0:
            return 'r_f'
        else:
            return 'b_f'

    def calculate(self, board):
        res = []
        row, line = self.position
        normalList = [[row+2, line+1], [row-2, line+1], [row+2, line-1], [row-2, line-1],\
        [row+1, line+2], [row-1, line+2], [row+1, line-2], [row-1, line-2]]
        extraList = [[row+3, line+3], [row-3, line-3], [row-3, line+3], [row+3, line-3]]
        for item in normalList:
            if self.isLegal(board, item):
                res.append(item)
        for extra in extraList:
            dx, dy = extra
            if dx >= 0 and dx <= const.ROW_MAX and dy >= 0 and dy <= const.LINE_MAX:
                if board[dx][dy] is None:
                    res.append(extra)
                elif board[dx][dy] is not None and board[dx][dy].getSide() != self.side:
                    res.append(extra)
        return res

class Elephants(Chess): #象
    def __init__(self, side):
        super().__init__(side)
        if side == 0:
            self.name = '相'
        else:
            self.name = '象'

    def __str__(self):
        if self.side == 0:
            return 'r_x'
        else:
            return 'b_x'

    def calculate(self, board):
        res = []
        row, line = self.position
        allList = [[row+2, line+2], [row-2, line+2], [row+2, line-2], [row-2, line-2]]
        for item in allList:
            if self.isLegal(board, item):
                res.append(item)
        return res
    def isLegal(self, board, destination):
        dx, dy = destination
        row, line = self.position
        if dx >= 0 and dx <= const.ROW_MAX and dy >= 0 and dy <= const.LINE_MAX:
            if board[dx][dy] is not None and board[dx][dy].getSide() == self.side:
                return False
            if board[int((row + dx) / 2)][int((line + dy) / 2)] is None:
                return True
        return False

class Mandarins(Chess): #士
    act = False
    def __init__(self, side):
        super().__init__(side)
        if side == 0:
            self.name = '仕'
        else:
            self.name = '士'

    def __str__(self):
        if self.side == 0:
            return 'r_s'
        else:
            return 'b_s'

    def calculate(self, board):
        res = []
        row, line = self.position
        List = [[row+1,line-1], [row+1,line+1], [row-1,line-1], [row-1,line+1]]
        for item in List:
            if self.act:
                if item[1] >= 0 and item[1] <= const.LINE_MAX and item[0] >= 0 and item[0] <= const.ROW_MAX:
                    if board[item[0]][item[1]] == None or board[item[0]][item[1]].getSide() != self.side:
                        res.append(item)
            elif item[1]>=3 and item[1]<=5 and ((item[0]>=0 and item[0]<=2) or (item[0]>=7 and item[0]<=9)):
                if board[item[0]][item[1]]==None or board[item[0]][item[1]].getSide() != self.side:
                    res.append(item)
        return res

class King(Chess): #帅
    def __init__(self, side):
        super().__init__(side)
        if side == 0:
            self.name = '帥'
        else:
            self.name = '将'

    def __str__(self):
        if self.side == 0:
            return 'r_j'
        else:
            return 'b_j'

    def calculate(self, board):
        res = []
        row, line = self.position
        List=[[row+1,line], [row-1,line], [row,line+1], [row,line-1]]
        for item in List:
            if self.side == 0 and item[1] >= 3 and item[1] <= 5 and item[0] >= 0 and item[0] <= 2:
                if board[item[0]][item[1]] is None or board[item[0]][item[1]].getSide() != self.side:
                    res.append(item)
            elif self.side == 1 and item[1] >= 3 and item[1] <= 5 and item[0] >= 7 and item[0] <= 9:
                if board[item[0]][item[1]] is None or board[item[0]][item[1]].getSide() != self.side:
                    res.append(item)
        if self.side == 0:
            tmp = row + 1
            while tmp >= 0 and tmp <= 9 and board[tmp][line] == None:
                tmp += 1
            if tmp >= 0 and tmp <= 9 and type(board[tmp][line]) == King:
                res.append([tmp,line])
        if self.side == 1:
            tmp = row - 1
            while tmp >= 0 and tmp <= 9 and board[tmp][line] == None:
                tmp -= 1
            if tmp >= 0 and tmp <= 9 and type(board[tmp][line]) == King:
                res.append([tmp,line])
        return res
    def isLegal(self, board):
        pass
        return True

class Pao(Chess): #炮
    def __init__(self, side):
        super().__init__(side)
        if side == 0:
            self.name = '砲'
        else:
            self.name = '炮'

    def __str__(self):
        if self.side == 0:
            return 'r_p'
        else:
            return 'b_p'

    def calculate(self, board):
        res=[]
        row,line=self.position
        dirc=[[1,0],[-1,0],[0,1],[0,-1]]
        for itemDirc in dirc:
            tmpr=row+itemDirc[0]
            tmpl=line+itemDirc[1]
            while tmpr>=0 and tmpr<=9 and tmpl>=0 and tmpl<=8 and board[tmpr][tmpl]==None:
                res.append([tmpr,tmpl])
                tmpr+=itemDirc[0]
                tmpl+=itemDirc[1]
            if tmpr>=0 and tmpr<=9 and tmpl>=0 and tmpl<=8 and board[tmpr][tmpl]!=None:
                tmpr+=itemDirc[0]
                tmpl+=itemDirc[1]
                while tmpr>=0 and tmpr<=9 and tmpl>=0 and tmpl<=8 and board[tmpr][tmpl]==None:
                    tmpr+=itemDirc[0]
                    tmpl+=itemDirc[1]
                if tmpr>=0 and tmpr<=9 and tmpl>=0 and tmpl<=8 and board[tmpr][tmpl]!=None and board[tmpr][tmpl].side!=self.side:
                    res.append([tmpr,tmpl])
        return res


    def isLegal(self, board):
        pass
        return True

class Pawns(Chess): #兵
    def __init__(self, side):
        super().__init__(side)
        if side == 0:
            self.name = '兵'
        else:
            self.name = '卒'
    def __str__(self):
        if self.side == 0:
            return 'r_z'
        else:
            return 'b_z'

    def calculate(self, board):
        res = []
        row, line = self.position
        if self.side == 0 and row < 5:
            if board[row+1][line] == None or board[row+1][line].side == 1:
                res.append([row+1,line])
            return res

        if self.side == 1 and row > 4:
            if board[row-1][line] == None or board[row-1][line].side == 0:
                res.append([row-1,line])
            return res

        List=[[row + 1,line],[row - 1,line],[row,line + 1],[row,line - 1]]
        for item in List:
            if self.side == 0 and item[0] >= row:
                if item[0] <= const.ROW_MAX and item[1] >= 0 and item[1] <= const.LINE_MAX:
                    if board[item[0]][item[1]] == None or board[item[0]][item[1]].side == 1:
                        res.append(item)
            elif self.side == 1 and item[0] <= row:
                if item[0] >= 0 and item[1] >= 0 and item[1] <= const.LINE_MAX:
                    if board[item[0]][item[1]] == None or board[item[0]][item[1]].side == 0:
                        res.append(item)
        return res
    def isLegal(self, board):
        return True

class Turtle(Chess): #龟
    def __init__(self, side):
        super().__init__(side)
        self.name = '龟'

    def __str__(self):
        if self.side == 0:
            return 'r_g'
        else:
            return 'b_g'

    def calculate(self, board):
        res = []
        row, line = self.position
        allList = [[row+2, line+2], [row-2, line+2], [row+2, line-2], [row-2, line-2]]
        for item in allList:
            if self.isLegal(board, item):
                res.append(item)
        return res

    def isLegal(self, board, destination):
        dx, dy = destination
        if dx >= 0 and dx <= const.ROW_MAX and dy >= 0 and dy <= const.LINE_MAX:
            if board[dx][dy] is not None and board[dx][dy].getSide() == self.side:
                return False
            return True
        return False

class Gun(Chess): #枪 - 跳走直吃
    def __init__(self, side):
        super().__init__(side)
        self.name = '枪'

    def calculate(self, board):
        res = []
        row, line = self.position
        direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
        for dir in direction:
            dx, dy = row, line
            moveFlag = False
            dx += dir[0]
            dy += dir[1]
            while dx >= 0 and dx <= const.ROW_MAX and dy >= 0 and dy <= const.LINE_MAX:
                if moveFlag:
                    if board[dx][dy] is None:
                        res.append([dx, dy])
                    else:
                        break
                elif board[dx][dy] is not None:
                    moveFlag = True
                    if board[dx][dy].getSide() != self.side:
                        res.append([dx, dy])
                dx += dir[0]
                dy += dir[1]
        return res

class Meng(Chess): #猛
    def __init__(self,side):
        super().__init__(side)
        self.name = '猛'

    def __str__(self):
        if self.side == 0:
            return 'r_e'
        else:
            return 'b_e'

    def calculate(self,board):
        res=[]
        row,line=self.position
        tmpDirc=[[1,0],[-1,0],[0,1],[0,-1]]
        for item in tmpDirc:
            x=row
            y=line
            while x+item[0] >= 0 and x+item[0] <= const.ROW_MAX and y+item[1] >= 0 and y+item[1] <= const.LINE_MAX:
                if board[x + item[0]][y + item[1]]!=None and board[x + item[0]][y + item[1]].side != self.side:
                    res.append([x + item[0], y + item[1]])
                    break
                elif board[x + item[0]][y + item[1]] == None:
                    res.append([x + item[0], y + item[1]])
                    x += item[0]
                    y += item[1]
                else:
                    break
        tmpDirc1=[[1,2],[1,-2],[2,1],[2,-1],[-1,2],[-1,-2],[-2,1],[-2,-1]]
        for item in tmpDirc1:
            if row+item[0]>=0 and row+item[0]<=const.ROW_MAX and line+item[1]>=0 and line+item[1]<=const.LINE_MAX:
                if board[row+item[0]][line+item[1]]!=None and board[row+item[0]][line+item[1]].side != self.side:
                    res.append([row+item[0],line+item[1]])
        return res

    def isLegal(self, board):
        return True

class Iskill(metaclass=abc.ABCMeta):
    def __init__(self, r, s): #构造函数
        self.myRound = r #回合数
        self.status = s #技能状态

    def getStr(self):
        return None

    def changeActive(self, roundCnt):#判断技能状态
        pass

    def skillEffectRound(self, chess):#回合数影响的技能
        pass

    def skillEffectBekilled(self, board, s, e):#被吃后触发的技能
        pass

class SowDiscord(Iskill):#离间
    def __init__(self):
        super(SowDiscord, self).__init__(15, 0)

    def getStr(self):
        if self.status == 1:
            return '挑拔离间(激活)'
        elif self.status == 2:
            return '挑拔离间(已生效)'
        else:
            return '挑拔离间(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        newChess = None
        if type(chess) == Elephants:
            newChess = Elephants(1 - chess.getSide())
        elif type(chess) == Mandarins:
            newChess = Mandarins(1 - chess.getSide())
        else:
            print('ERROR' + str(chess) + '错误子类')
        newChess.setPosition(chess.position)
        self.status = 2
        return newChess

    def skillEffectBekilled(self, board, s, e):
        pass

class Evolve(Iskill):#进化
    def __init__(self):
        super(Evolve, self).__init__(10, 0)

    def getStr(self):
        if self.status == 1:
            return '平步青云(激活)'
        elif self.status == 2:
            return '平步青云(已生效)'
        else:
            return '平步青云(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        newChess = None
        if type(chess) == Rooks:
            newChess = Meng(chess.getSide())
        elif type(chess) == Mao or type(chess) == Pao:
            newChess = Demon(chess.getSide())
        elif type(chess) == Elephants:
            newChess = Turtle(chess.getSide())
        elif type(chess) == Pawns:
            newChess = Fire(chess.getSide())
        else:
            print('ERROR' + str(chess) + '不能转化')
        newChess.setPosition(chess.position)
        self.status = 2
        return newChess

    def skillEffectBekilled(self, board, s, e):
        pass

class Invisible(Iskill):#隐形
    def __init__(self):
        super(Invisible, self).__init__(5, 0)

    def getStr(self):
        if self.status == 1:
            return '神出鬼没(激活)'
        else:
            return '神出鬼没(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        chess.name = '' #简单隐形

    def skillEffectBekilled(self, board, s, e):
        pass

class Poison(Iskill):#毒
    def __init__(self):
        super(Poison, self).__init__(10, 0)

    def getStr(self):
        if self.status == 1:
            return '同归于尽(激活)'
        else:
            return '同归于尽(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        pass

    def skillEffectBekilled(self, board, s, e):
        if self.status == 1:
            board[s[0]][s[1]] = board[e[0]][e[1]] = None
            return True

class Bomb(Iskill):#炸弹
    def __init__(self):
        super(Bomb, self).__init__(20, 0)

    def getStr(self):
        if self.status == 1:
            return '紫电青霜(激活)'
        elif self.status == 2:
            return '紫电青霜(已失效)'
        else:
            return '紫电青霜(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        pass

    def skillEffectBekilled(self, board, s, e):
        if self.status == 1:
            self.status = 2
            n = 3
            dirx=[[1,1],[1,-1],[-1,-1],[-1,1]]
            while n>0:
                for item in dirx:
                    if e[0]+item[0]>=0 and e[0]+item[0]<=9 and e[1]+item[1]<=0 and e[1]+item[1]<=8:
                        tmpx=e[0]+item[0]
                        tmpy=e[1]+item[1]
                        if board[tmpx][tmpy]!=None and board[tmpx][tmpy].skillName==None:
                            board[tmpx][tmpy]=None
                        elif board[tmpx][tmpy]!=None and type(board[tmpx][tmpy].skillName)==Resurgence:#持有复活的人被炸到
                            pass
            return True

class Resurgence(Iskill): #复活
    placeList = {
        Rooks: [[[0,0],[0,8]],[[9,0],[9,8]]],
        Pao: [[[2,1],[2,7]],[[7,1],[7,7]]],
        Mao: [[[0,1],[0,7]],[[9,1],[9,7]]],
        Elephants: [[[0,2],[0,6]],[[9,2],[9,6]]],
        Mandarins: [[[0,3],[0,5]],[[9,3],[9,5]]],
        Pawns: [[[3,0],[3,2],[3,4],[3,6],[3,8]],[[6,0],[6,2],[6,4],[6,6],[6,8]]]
    }
    def __init__(self):
        super(Resurgence, self).__init__(0, 1)

    def getStr(self):
        if self.status == 1:
            return '阴魂不散(激活)'
        elif self.status == 2:
            return '阴魂不散(已失效)'
        else:
            return '阴魂不散(' + str(self.myRound) + '回合后激活)'

    def changeActive(self, roundCnt):
        if roundCnt >= self.myRound:
            self.status = 1
        return self.status

    def skillEffectRound(self, chess):
        pass

    def skillEffectBekilled(self, board, s, e): #这里不全
        if self.status == 1:
            self.status == 2
            chess = board[e[0]][e[1]]
            newChess = None
            if type(chess) == Rooks:
                newChess = Rooks(chess.getSide())
            elif type(chess) == Pao:
                newChess = Pao(chess.getSide())
            elif type(chess) == Elephants:
                newChess = Elephants(chess.getSide())
            elif type(chess) == Pawns:
                newChess = Pawns(chess.getSide())
            elif type(chess) == Mao:
                newChess = Mao(chess.getSide())
            elif type(chess) == Mandarins:
                newChess = Mandarins(chess.getSide())
            else:
                print('ERROR' + str(chess) + '类型不匹配')
            for item in self.placeList[type(chess)][chess.getSide()]:
                if board[item[0]][item[1]] is None:
                    board[item[0]][item[1]] = newChess
                    newChess.setPosition(item)
                    return item

def stest():
    sn = Evolve()
    sn.changeActive(30)
    chess = Rooks(1)
    chess.setSkill(sn)
    return chess
